// pages/yearDetail/yearDetail.js
import {
  hgetStorage,
  hnavigateBack,
  hshowToast
} from "../../static/htools.wx";
import {
  hrequestGet,
  hrequestPut
} from "../../static/hunit.wx";
import api from '../../assets/api'
const {
  DeclareYear,
  DeclareYearId
} = api
import WxValidate from '../../static/WxValidate'
Page({
  data: {
    isSubmit:false,
    queryId: '',
    submitData: [{
      month: "january",
      curValue: "",
      nextValue: ""
    }, {
      month: "february",
      curValue: "",
      nextValue: ""
    }, {
      month: "march",
      curValue: "",
      nextValue: ""
    }, {
      month: "april",
      curValue: "",
      nextValue: ""
    }, {
      month: "may",
      curValue: "",
      nextValue: ""
    }, {
      month: "june",
      curValue: "",
      nextValue: ""
    }, {
      month: "july",
      curValue: "",
      nextValue: ""
    }, {
      month: "august",
      curValue: "",
      nextValue: ""
    }, {
      month: "september",
      curValue: "",
      nextValue: ""
    }, {
      month: "october",
      curValue: "",
      nextValue: ""
    }, {
      month: "november",
      curValue: "",
      nextValue: ""
    }, {
      month: "december",
      curValue: "",
      nextValue: ""
    }],
    declareYearDto: {},
    declareYearDtoLast: {},
  },
  onLoad: function (options) {
    this.initValidate() //验证规则函数
    this.setData({
        queryId: options.id
      }),
      this.getYearDataById()
  },
  showModal(error) {
    wx.showModal({
      content: error.msg,
      showCancel: false,
    })
  },
  initValidate() {
    const rules = {
      num0: {
        required: true,
        number: true
      },
      num1: {
        required: true,
        number: true
      },
      num2: {
        required: true,
        number: true
      },
      num3: {
        required: true,
        number: true
      },
      num4: {
        required: true,
        number: true
      },
      num5: {
        required: true,
        number: true
      },
      num6: {
        required: true,
        number: true
      },
      num7: {
        required: true,
        number: true
      },
      num8: {
        required: true,
        number: true
      },
      num9: {
        required: true,
        number: true
      },
      num10: {
        required: true,
        number: true
      },
      num11: {
        required: true,
        number: true
      }
    }
    const messages = {
      num0: {
        required: '请输入1月预估电量',
        number: '请输入数字'
      },
      num1: {
        required: '请输入2月预估电量',
        number: '请输入数字'
      },
      num2: {
        required: '请输入3月预估电量',
        number: '请输入数字'
      },
      num3: {
        required: '请输入4月预估电量',
        number: '请输入数字'
      },
      num4: {
        required: '请输入5月预估电量',
        number: '请输入数字'
      },
      num5: {
        required: '请输入6月预估电量',
        number: '请输入数字'
      },
      num6: {
        required: '请输入7月预估电量',
        number: '请输入数字'
      },
      num7: {
        required: '请输入8月预估电量',
        number: '请输入数字'
      },
      num8: {
        required: '请输入9月预估电量',
        number: '请输入数字'
      },
      num9: {
        required: '请输入10月预估电量',
        number: '请输入数字'
      },
      num10: {
        required: '请输入11月预估电量',
        number: '请输入数字'
      },
      num11: {
        required: '请输入12月预估电量',
        number: '请输入数字'
      }
    }
    this.WxValidate = new WxValidate(rules, messages)
  },
  // 表单验证并提交申报
  async formSubmit(e) {
    const params = e.detail.value
    //校验表单
    if (!this.WxValidate.checkForm(params)) {
      const error = this.WxValidate.errorList[0]
      console.log(this.WxValidate.errorList)
      this.showModal(error)
      return false
    }
    let _dataJson = {}
    this.data.submitData.forEach(item => {
      _dataJson[item.month] = item.nextValue
    });
    _dataJson.deptId = hgetStorage("deptId");
    _dataJson.id = this.data.queryId;
    _dataJson.status = 1;
    _dataJson.declareYear = this.data.declareYearDto.declareYear;
    let _res = await hrequestPut(DeclareYear, _dataJson);
    console.log(_res)
    this.setData({
      isSubmit:true
    })
    if (_res.statusCode === 204) {
      hshowToast('提交成功')
      let pages = getCurrentPages(); //定义上页面
      let prevPage = pages[pages.length - 2]; //上一个页面
      if (prevPage) {
        //上一页存在则给上一个data赋值。则把title传过去
        prevPage.setData({
          isYearUpdate: true,
          yearPage:0,
          isShowYearMore:true
        });
      }

      hnavigateBack(1)
    } else {
      hshowToast('数据过长请重新输入提交')
    }
  },
  // 传入Id查询数据
  async getYearDataById() {
    let _id = this.data.queryId;
    let _res = await hrequestGet(DeclareYearId + _id);
    console.log(_res);
    let declareYearDto = _res.data.declareYearDto === null ? {} : _res.data.declareYearDto
    let declareYearDtoLast = _res.data.declareYearDtoLast === null ? {} : _res.data.declareYearDtoLast
    this.setData({
      declareYearDto,
      declareYearDtoLast
    })
    this.initData();
  },
  initData() {
    // 数据处理
    this.data.submitData.forEach((item, index) => {
      let _curValue = "submitData[" + index + "].curValue";
      let _nextValue = "submitData[" + index + "].nextValue";
      this.setData({
        [_nextValue]: this.data.declareYearDto[item.month] || '',
        [_curValue]: this.data.declareYearDtoLast[item.month] || '-'
      })
    });
  },
  // 双向绑定input数据
  inputChange(e) {
    let index = e.currentTarget.dataset.mouth;
    let nextValue = 'submitData[' + index + '].nextValue'
    this.setData({
      [nextValue]: e.detail.value
    })
  },
  blur(e) {
    let _index = e.currentTarget.dataset.index;
    let _item = e.currentTarget.dataset.item;
    let _value = e.currentTarget.dataset.value;
    if (_value === '' && _item.curValue !== '-') {
      let nextValue = 'submitData[' + _index + '].nextValue'
      this.setData({
        [nextValue]: _item.curValue
      })
    }
  }
})